package org.viva.action.cms;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.viva.entity.cms.ContentRes;
import org.viva.entity.cms.Movie;
import org.viva.service.cms.ContentResService;
import org.viva.service.cms.MovieService;
import org.viva.service.cms.TagService;
import org.viva.service.sys.WebsiteCache;

import net.spider.bttiantang.MovieDetail;
import net.spider.bttiantang.Page;
import net.spider.bttiantang.Picture;

@Controller
public class SpiderAction {

    @Resource
    private MovieService      movieService;
    @Resource
    private TagService        tagService;
    @Resource
    private ContentResService contentResService;

    @RequestMapping("spider/bt")
    @ResponseBody
    public String bt() throws Exception {
        String baseUrl = "http://www.bttiantang.com";
        Page page = new Page();
        int pageCount = 5;// page.getPageCount(baseUrl);
        String urlWithPageParamater = "";
        for (int i = pageCount; i > 0; i--) {
            if (i == 0) {
                break;
            }
            try {
                // 这里可以用多线程去抓
                if (i != 1) {
                    urlWithPageParamater = baseUrl + "?PageNo=" + i;
                } else {
                    urlWithPageParamater = baseUrl;
                }
                List<Movie> movies = page.getMovieFormPage(urlWithPageParamater);
                System.out.println(urlWithPageParamater + "-----------------------------------" + "第" + i + "页：共" + movies.size() + "条数据");
                for (int j = movies.size() - 1; j >= 0; j--) {
                    System.out.println(movies.get(j).getSurl());
                    try {
                        MovieDetail d = new MovieDetail();
                        Movie movie = d.getMovie(movies.get(j));
                        movie.setCgId(1L);
                        movieService.add(movie);
                        for (ContentRes res : movie.getContentRes()) {
                            res.setCid(movie.getId());
                            contentResService.add(res);
                        }
                        tagService.setTags(movie.getTags());
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            } catch (Exception e) {
                throw new Exception("第" + i + "页");
            }
        }
        return "over!!!";
    }

    @RequestMapping("spider/pic")
    @ResponseBody
    public String doPic(HttpServletRequest request) throws Exception {
        String root = request.getSession().getServletContext().getRealPath("/") + "/oss/imdb";
        Picture pic = new Picture();
        List<Movie> list = movieService.pic(true);
        for (Movie movie : list) {
            try {
                if (StringUtils.isEmpty(movie.getImdb())) continue;
                pic.getFromUrl(root, "http://www.imdb.com/title/" + movie.getImdb() + "/", movie.getId());
                movie.setImg(WebsiteCache.get("sys.host") + "/oss/down?path=/imdb/" + movie.getImdb() + "-" + movie.getId() + "sm.jpg");
            } catch (Exception e) {
                movie.setImg("error");
                e.printStackTrace();
            }
        }
        return "over!!!";
    }

    @RequestMapping("spider/clear")
    @ResponseBody
    public String doClearPic(HttpServletRequest request) throws Exception {
        String root = request.getSession().getServletContext().getRealPath("/") + "/oss";
        List<Movie> list = movieService.pic(false);
        for (Movie movie : list) {
            try {
                File f = new File(root + movie.getImg());
                if (!f.exists()) {
                    movie.setImg(null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "over!!!";
    }

    @RequestMapping("spider/ct")
    @ResponseBody
    public String doct(HttpServletRequest request) throws Exception {
        tagService.clearTags();
        return "over!!!";
    }

}
